package com.example.designModel.threadLoading;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LoadGame {


    public static void main(String[] args) throws InterruptedException {

        Resouce resouce = new Resouce();
        LoadGameThread loadGameThread = new LoadGameThread(resouce);
        Clearance clearance = new Clearance(resouce);
        long starttime = System.currentTimeMillis();
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        executorService.execute(loadGameThread);
        executorService.execute(clearance);
        executorService.awaitTermination(1, TimeUnit.SECONDS);
        executorService.shutdownNow();
        long endTime = System.currentTimeMillis();
        System.out.println("用时:"+(endTime - starttime-1000)+"毫秒");

    }

}

class Resouce{
    private int g = 0;
    private volatile boolean isbackgroundMusic;
    private volatile boolean isMapData;
    private volatile boolean isRoleModel;
    private volatile boolean isOther;
    private volatile boolean canPass;

    private final Condition condition;
    private final Lock lock;

    Resouce()
    {
        canPass = false;
        isbackgroundMusic = false;
        isMapData = false;
        isRoleModel = false;
        isOther = false;
        lock = new ReentrantLock();
        condition = lock.newCondition();
    }

    Lock getLock()
    {
        return lock;
    }
    void LoadResource()
    {
        lock.lock();
        try
        {

            while (canPass) {
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (!isbackgroundMusic) {
                isbackgroundMusic = true;
                System.out.println("加载背景音乐");
            }
            if (!isMapData) {
                isMapData = true;
                System.out.println("加载地图数据");
            }
            if (!isRoleModel) {
                isRoleModel = true;
                System.out.println("加载角色模型");
            }
            if (!isOther) {
                isOther = true;
                System.out.println("加载其他资源");
            }
            if (isbackgroundMusic && isRoleModel && isMapData && isOther) {
                this.g += 1;
                canPass = true;
                isbackgroundMusic = false;
                isRoleModel = false;
                isMapData = false;
                isOther = false;
                condition.signal();
            }

        }
        finally
        {
            lock.unlock();
            if (this.g > 5) {
                System.out.println("恭喜你通关了,游戏结束");
                Thread.interrupted();
            }

        }
    }
    int pass()
    {
        lock.lock();
        try
        {
            while (!canPass) {
                try
                {
                    condition.await();
                }
                catch (InterruptedException ie)
                {
                    ie.printStackTrace();
                }
            }
            canPass = false;
            condition.signalAll();
        }
        finally
        {
            lock.unlock();
            return g;
        }
    }
}

class LoadGameThread implements Runnable
{
    private final Lock l;

    private final Resouce resouce;

    LoadGameThread(Resouce s)
    {
        this.resouce = s;
        l = s.getLock();
    }
    @Override
    public void run() {
        for (int i = 0;i < 6;i++) {
            l.lock();
            try {
                resouce.LoadResource();
            }finally {
                l.unlock();
            }

        }
    }
}
class Clearance implements Runnable{

    private final Lock l;

    private final Resouce s;

    Clearance(Resouce s)
    {
        this.s = s;
        l = s.getLock();
    }

    @Override
    public void run()
    {
        int guan;
        do
        {
            l.lock();
            try {
                guan = s.pass();
                System.out.println("开始游戏,进入第"+guan+"关");
            } finally {
                l.unlock();
            }
        }
        while (guan != 5);
    }
}

